import type { Middleware } from '@nuxt/types'
import cookie from 'cookie'
import { LocalKeys } from '@/enum/constant'
import { ActionsType } from '@/actions/user'
// import { Message } from 'element-ui'
const mdw: Middleware = async function ({ store, redirect, req }) {
  if (process.client) {
    if (!store.getters['user/authenticated']) {
      // await store.dispatch(ActionsType.getUserInfo)
      return redirect('/login')
    }
  }
  if (process.server) {
    // console.log(typeof req.headers.cookie === 'string')
    if (typeof req.headers.cookie === 'string') {
      const cookieMap = cookie.parse(req.headers.cookie)
      const token = cookieMap[LocalKeys.AccessToken]
      if (!token) {
        // await store.dispatch(ActionsType.getUserInfo)
        return redirect('/login')
      }
    } else {
      redirect('/login')
    }
  }
}

export default mdw
